//
//  Router.swift
//  AlamofireRouter
//
//  Created by 李煜 on 2024/6/18.
//https://leon_lizi.gitbooks.io/alamofire/content/chapter1.html

import Alamofire
import UIKit

enum Router: URLRequestConvertible {
    func asURLRequest() throws -> URLRequest {
        let result: (path: String, parameters: Parameters) = {
            switch self {
                case .PopularPhotos(let page):
                    let params = ["consumer_key": Router.consumerKey, "page": "\(page)", "feature": "popular", "rpp": "50",  "include_store": "store_download", "include_states": "votes"]
                    return ("/photos", params)
                case .PhotoInfo(let photoID, let imageSize):
                    let params = ["consumer_key": Router.consumerKey, "image_size": "\(imageSize)"]
                    return ("/photos/\(photoID)", params)
                case .Comments(let photoID, let commentsPage):
                    let params = ["consumer_key": Router.consumerKey, "comments": "1", "comments_page": "\(commentsPage)"]
                    return ("/photos/\(photoID)/comments", params)
            }
        }()
        
        let url = URL(string: Router.baseURLString)!.appendingPathComponent(result.path)
        
        let request = URLRequest(url: url)
        
        return try URLEncoding.default.encode(request, with: result.parameters)
    }
    
    static let baseURLString = "https://api.500px.com/v1"
    static let consumerKey = "消费者密钥"
    
    case PopularPhotos(Int)
    case PhotoInfo(Int, Int)
    case Comments(Int, Int)
    
    
}
